package com.aoyy.dao;

import java.util.List;
import org.springframework.stereotype.Repository;
import com.aoyy.model.Post;
import com.aoyy.util.Page;
/**=================================================================================================
 * DAO class <code>PostDao</code>
 * @author this author
 * @date 2017-08-06
 * ================================================================================================== */
@Repository
public class PostDao extends BaseDao<Post> {
	// Query post list by topicId
	public List<Post> queryPostsByTopicId(Integer topicId) {
		String hql = "from Post where topic.topicId = " + topicId + " order by createTime desc";
		@SuppressWarnings("unchecked")
		List<Post> list = this.find(hql);
		return list;
	}
	
	// Paged Query posts by topicId
	public Page pagedQuery(Integer pageNo, Integer pageSize, Integer topicId) {
		String hql = "from Post where topic.topicId = " + topicId + " order by createTime desc";
		return this.pagedQuery(hql, pageNo, pageSize);
	}
	
	// Query post by postId 
	public Post queryPostByPostId(int postId) {
		String hql = "from Post where postId = " + postId;
		@SuppressWarnings("rawtypes")
		List list = this.find(hql);
		if (list != null && !list.isEmpty()) {
			return (Post) list.get(0);
		}
		return null;
	}

}
